package com.tb.controller;

import com.github.pagehelper.PageInfo;
import com.tb.model.Answer;
import com.tb.model.Follow;
import com.tb.service.AnswerService;
import com.tb.service.FollowService;
import com.tb.service.UserService;
import com.tb.util.MyUtils;
import com.tb.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @description: 首页相关
 * @author: wangguichun
 * @create: 2018-04-11 23:36
 **/

@Controller
public class IndexController {

    @Autowired
    private UserService userService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private FollowService followService;

    @RequestMapping({"/","index"})
    public String index(Model model, HttpServletRequest request){
        return "redirect:index/1";
    }

    @RequestMapping("index/{pageId}")
    public String answerList(@PathVariable Integer pageId, Model model, HttpServletRequest request){
        int fromUserId = Integer.parseInt(MyUtils.getCookie(request, "id").getValue());
        List<Follow> followList = followService.getListByFormUserId(fromUserId);
        PageInfo<Answer> pageInfo = answerService.getAnswerByFollowListAndPageId(followList,pageId);
//        PageBean<Answer> pageBean = new PageBean<>();
//        pageBean.setCurPage(pageId);
//        int pageCount = answerService.getCountOfAnswerByFollowList(followList) / MyConstant.PAGE_SIZE_ANSWER + 1;
//        pageBean.setAllPage(pageCount);
//        pageBean.setList(answerList);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);

        return "index";
    }

    @RequestMapping("login")
    public String login(){
        return "login";
    }

    @RequestMapping("loginCheck")
    @ResponseBody
    public Response loginCheck(String email, String password, HttpServletResponse response) {
        Map<String, Object> map = userService.loginCheck(email, password, response);
        if (map.get("error") != null) {
            return new Response(0, map.get("error").toString());
        } else {
            return new Response(1, "", map);
        }
    }

    @RequestMapping("register")
    @ResponseBody
    public Response register(String username, String email, String password) {
        Map<String, String> map = userService.register(username, email, password);
        if (map.get("ok") != null) {
            return new Response(0, "系统已经向你的邮箱发送了一封邮件哦，验证后就可以登录啦~");
        } else {
            return new Response(1, "error", map);
        }
    }

    @RequestMapping("activateUser")
    public String activateUser(int userId) {
        userService.activateUser(userId);
        return "redirect:index";
    }
}
